package com.ruoyi.mobile.project.ums.service;


import com.ruoyi.db.ums.domain.UmsAddress;
import com.ruoyi.db.ums.domain.query.UmsAddressQueryVo;
import com.ruoyi.db.ums.mapper.UmsAddressMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 任作炜
 */
@Service
@Transactional
public class WxUmsAddressService
{
    @Resource
    private UmsAddressMapper addressMapper;

    //查询收货地址列表
    public  List<UmsAddress> queryByUid(Long id)
    {
        UmsAddressQueryVo addressQueryVo = new UmsAddressQueryVo();
        addressQueryVo.setUserId(id);
        return addressMapper.selectUmsAddressList(addressQueryVo);
    }

    //查询收货地址
    public UmsAddress query(Long userId, Long id)
    {
        //第一步确定是建立一个查询实体对象
        UmsAddressQueryVo addressQueryVo = new UmsAddressQueryVo();
        //第二步设置userId确定是查哪个用户的，因为数据库里面存了很多人，即有很多userId要确定是谁的
        //判断查询到的userID是否与自己用户的userID对应
        addressQueryVo.setUserId(userId);
        //判断查询到的userID是否与自己用户的userID对应
        //在查询那个地址的id
        return addressMapper.selectUmsAddressById(id);
    }

    //新增收货地址
    public int add(UmsAddress address)
    {
        address.setAddTime(LocalDateTime.now());
        address.setUpdateTime(LocalDateTime.now());
        return addressMapper.insertUmsAddress(address);
    }

    //修改收货地址
    public int update(UmsAddress address)
    {
        address.setUpdateTime(LocalDateTime.now());
        return addressMapper.updateUmsAddress(address);
    }

    //删除收货地址
    public void delete(Long id)
    {
        addressMapper.deleteUmsAddressById(id);
    }

    public UmsAddress findDefault(Long userId)
    {
        UmsAddressQueryVo addressQueryVo = new UmsAddressQueryVo();
        addressQueryVo.setUserId(userId);
        return addressMapper.selectUmsAddressByUserId(userId);
    }

    public void resetDefault(Long userId)
    {
        UmsAddress address = new UmsAddress();
        address.setIsDefault(false);
        address.setUpdateTime(LocalDateTime.now());
        UmsAddressQueryVo addressQueryVo = new UmsAddressQueryVo();
        addressQueryVo.setUserId(userId);
        addressMapper.updateUmsAddress(address);
    }

    public List<UmsAddress> querySelective(Long userId, String name, Integer page, Integer limit, String sort, String order)
    {
        return null;
    }
}
